import java.util.HashSet;
import java.util.Set;

/**
 * Created whit IntelliJ IDEA.
 * Description：
 * User：ZHONGCHEN
 * Date:2022-05-13
 * Time:12:16
 */

/**
 * 动态规划
 */
public class Main {

    //分裂字符串--状态:字符串前i个字符是否可以被分割
    public static void main1(String[] args) {
        String s = "leetcode";
        Set<String> set = new HashSet<>();
        set.add("lee");
        set.add("t");
        set.add("code");
        System.out.println(wordBreak(s, set));
    }

    public static boolean wordBreak(String s, Set<String> dic) {
        boolean[] ans = new boolean[s.length() + 1];
        ans[0] = true;
        for (int i = 1; i <= s.length(); i++) {
            for (int j = 0; j < i; j++) {
                String c = s.substring(j, i);
                if (ans[j] && dic.contains(s.substring(j, i))) {
                    ans[i] = true;
                    break;
                }
            }
        }
        return ans[s.length()];
    }

    /**
     * 字符串切割
     */
    public static void main(String[] args) {
        System.out.println(minCut("aabaa"));
    }

    public static int minCut(String s) {
        if (s.length() == 0) {
            return 0;
        }
        int len = s.length();
        int[] cut = new int[len + 1];
        //初始化
        for (int i = 0; i < cut.length; i++) {
            cut[i] = i - 1;
        }

        for (int i = 1; i <= len; i++) {
            for (int j = 0; j < i; j++) {
                if (isPalindrome(s,j,i-1)){
                    cut[i] = Math.min(cut[i],1+cut[j]);
                }
            }
        }
        return  cut[len];
    }

    private static boolean isPalindrome(String s, int i, int j) {
        while (i < j) {
            if (s.charAt(i) != s.charAt(j)){
                return false;
            }
            i++;
            j--;
        }
        return true;
    }
}
